﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KduneeShaders;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ShaderApp
{
    class ShaderedObject
    {
        GeneralShader shader;
        Model model;
        Matrix obj2world = Matrix.CreateRotationY(3.14f / 4.0f);

        public Matrix Obj2World
        {
            get { return obj2world; }
            set { obj2world = value; }
        }

        public ShaderedObject(GeneralShader shader, Model model)
        {
            this.shader = shader;
            this.model = model;
        }

        public void Draw(GameTime gameTime, Vector3 eyePosition, Vector3 lightPosition, Matrix world2projection)
        {
            shader.Update(gameTime.ElapsedGameTime.Milliseconds, eyePosition, lightPosition);
            foreach (ModelMesh m in model.Meshes)
                foreach (ModelMeshPart p in m.MeshParts)
                    p.Effect = shader;

            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                Matrix obj2world = transforms[mesh.ParentBone.Index] * this.obj2world;

                shader.SetMatrices(obj2world, world2projection);

                mesh.Draw();
            }
        }
    }
}
